package custom;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeSelectionModel;

import net.miginfocom.swing.MigLayout;

import atessitura.TFController;

public class JarFileChooser extends JFrame implements TreeSelectionListener{
	
	private BackgroundPanel background;
	private TFController control;
	
	private String file;
	
	private JarFile jar;
	
	private JTree tree;
	
	public JarFileChooser(JarFile file)
	{
		this(file,null, null);
	}
	
	public JarFileChooser(JarFile jarFile, String ffile, TFController cntrl)
	{
		super("File Chooser");
		
		file=ffile;
		
		control=cntrl;
		
		jar=jarFile;
		
		BufferedImage carbonFibre = null;
		try {
			carbonFibre = ImageIO.read(JarFileChooser.class.getResource("/resources/images/honey_im_subtle.png"));
		} catch (IOException e) {
		}
		
		background=new BackgroundPanel(carbonFibre);
		background.setStyle(1);
		MigLayout miglayout=new MigLayout("fill");
		//BorderLayout borderlayout=new BorderLayout();
		background.setLayout(miglayout);

		setContentPane(background);
		
		DefaultMutableTreeNode top=new DefaultMutableTreeNode(file);
		
		DefaultMutableTreeNode song=null;
		for(Enumeration e=jarFile.entries();e.hasMoreElements();)
		{
			JarEntry now=(JarEntry)e.nextElement();
			if(now.toString().contains(file))
			{
				String songString=now.toString().substring(now.toString().indexOf(file+"/")+(file.length()+1));
				if(songString.length()>1)
				{
					song=new DefaultMutableTreeNode(songString);
					top.add(song);
				}
				
			}
		}
		
		tree=new JTree(top);
		tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
		tree.addTreeSelectionListener(this);
		background.add(tree,"wrap");
		
		JButton okay=new JButton("Okay");
		okay.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				DefaultMutableTreeNode node = (DefaultMutableTreeNode)
	                       tree.getLastSelectedPathComponent();

			    if (node == null)
			    //Nothing is selected.     
			    return;
		
			    Object nodeInfo = node.getUserObject();
			    if (node.isLeaf()) {
			        String songString = (String)nodeInfo;
			        for(Enumeration e=jar.entries();e.hasMoreElements();)
					{
						JarEntry now=(JarEntry)e.nextElement();
						
						if(now.toString().contains(songString))
						{
							control.openExampleFilePicked(jar, now);
							WindowEvent wev = new WindowEvent(JarFileChooser.this, WindowEvent.WINDOW_CLOSING);
			                Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
							return;
						}
					}
			        //displayURL(book.bookURL);
			    } else {
			        //displayURL(helpURL); 
			    }
			}
			
		});
		//background.add(okay);
		
		setBounds(600, 300, 300, 300);
		this.setVisible(true);
	}

	@Override
	public void valueChanged(TreeSelectionEvent ee) {
		// TODO Auto-generated method stub
		DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                tree.getLastSelectedPathComponent();

	    if (node == null)
	    //Nothing is selected.     
	    return;

	    Object nodeInfo = node.getUserObject();
	    if (node.isLeaf()) {
	        String songString = (String)nodeInfo;
	        for(Enumeration e=jar.entries();e.hasMoreElements();)
			{
				JarEntry now=(JarEntry)e.nextElement();
				//System.out.println(now);
				if(now.toString().contains(songString) && now.toString().contains(file))
				{
					control.openExampleFilePicked(jar, now);
					WindowEvent wev = new WindowEvent(JarFileChooser.this, WindowEvent.WINDOW_CLOSING);
	                Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
					return;
				}
			}
	        //displayURL(book.bookURL);
	    } else {
	        //displayURL(helpURL); 
	    }
	}
}
